using UnityEngine;
using System.Collections;

public class DeathBallController: MonoBehaviour 
{



	
	
	
	float m_fSpeed = 10.5f;
	bool m_bIphone = false;
	Vector3 PreviousPosition;
	
	void OnCollisionEnter( Collision _Collider)
	{
		if (_Collider.gameObject.name == "Wall")
		{
			Vector3 VecToUs = PreviousPosition - _Collider.contacts[_Collider.contacts.Length-1].point;
			GameObject Temp = GameObject.Find("Sparks");
			Temp.transform.position = _Collider.contacts[0].point;
			Temp.particleEmitter.emit = true;
			Temp.particleEmitter.localVelocity = (VecToUs * 5.0f);

			//GameObject.Find("GUIScoreObj").SendMessage("IncreaseScore", -1);

			
			
		}
		if(_Collider.gameObject.tag == "Ball" || _Collider.gameObject.tag == "BigBall")
		{
			Destroy(_Collider.gameObject);
		}
		
		
	}
	void OnCollisionExit(Collision _Collider)
	{
		if (_Collider.gameObject.name == "Wall")
		{
			print("Hit Wall");
			//Vector3 VecToUs = PreviousPosition - _Collider.contacts[0].point;
			GameObject Temp = GameObject.Find("Sparks");
			//Temp.transform.position = _Collider.contacts[0].point;
			Temp.particleEmitter.emit = false;
			//Temp.particleEmitter.localVelocity = (VecToUs * 5.0f);


		}
	}
	// Use this for initialization
	void Start () {
		//GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate;
	}
	
	// Update is called once per frame
	void Update () 
	{
		
		//if (GetComponent<Rigidbody>().interpolation == RigidbodyInterpolation.Interpolate)
		//{
		//    GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.None;
		//}
		//else
		//    GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate;
		PreviousPosition = transform.position;
	
	}
	void FixedUpdate()
	{
		if (m_bIphone)
		{
			rigidbody.AddForce(Input.acceleration, ForceMode.Acceleration);
			//if(Input.GetTouch(0))
			//rigidbody.AddForce(Input.acceleration, ForceMode.Impulse);
		}
		else
		{
			rigidbody.AddForce(Input.GetAxis("Horizontal") * m_fSpeed, 0.0f, Input.GetAxis("Vertical") * m_fSpeed, ForceMode.Force);
			if (Input.GetButton("Fire2"))
			{
				rigidbody.AddForce(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"), ForceMode.Impulse);
				//rigidbody.AddForce(transform.forward, ForceMode.Impulse);
			
			}
		}
		//transform.position = (new Vector3(transform.position.x + Input.GetAxis("Horizontal"), transform.position.y, transform.position.z + Input.GetAxis("Vertical")));
	}
}


